use winapi::um::winuser::{SW_SHOWNORMAL};
use crate::utils::exec;
use std::env;
use std::path::Path;
use crate::prompt_theme::MyTheme;
use dialoguer::Select;

// 打开文件
pub struct  Open{}

impl Open{
    pub fn run(&self,filename: &str) {
        match filename {
            // 为空时，打开提示
            "" => {
                let selections = &[
                    "hosts file",
                    "cargo config",
                    "git config",
                    "regedit",
                    "Environment variable",
                    "services msc"
                ];

                let selection = Select::with_theme(&MyTheme::custom())
                    .with_prompt("请选择要打开的文件:")
                    .items(&selections[..])
                    .interact_opt()
                    .unwrap();
                if let Some(selection) = selection {
                    // println!("selection is {}",selection);
                    // println!("选项：{}",selections[selection]);
                    match selection {
                        0 => self.hosts(),
                        1 => self.cargo(),
                        2 => self.git(),
                        3 => self.regedit(),
                        4 => self.env(),
                        5 => self.service(),
                        _ => println!("没有匹配到 {} 或暂时不支持",selections[selection])
                    }
                }else {
                    println!("你没有选择任何选项")
                }
            },
            "hosts" | "h" => self.hosts(),
            "cargo" | "rust" => self.cargo(),
            "git" => self.git(),
            "regedit" => self.regedit(),
            "env" => self.env(),
            "service" | "s" => self.service(),

            _ => println!("没有匹配到 {} 或暂时不支持",filename)
        }
    }
    /// 打开hosts 文件
    pub fn hosts(&self) {

       // self.run("runas","C:\\Windows\\Notepad.exe","C:\\Windows\\System32\\drivers\\etc\\hosts","SW_SHOWNORMAL");
        exec("runas","C:\\Windows\\Notepad.exe","C:\\Windows\\System32\\drivers\\etc\\hosts",SW_SHOWNORMAL);
    }
    /// 打开cargo file
    pub fn cargo(&self) {
        // 判断文件是否存在
        let cargo_path = env::var("USERPROFILE").unwrap() + "\\.cargo\\config";
        if !Path::new(&cargo_path).exists() {
            println!("{} not exists",cargo_path)
        } else {
            // 打开文件
            exec("runas","C:\\Windows\\Notepad.exe",cargo_path.as_str(),SW_SHOWNORMAL);
        }

    }

    /// 打开注册表
    pub fn regedit(&self) {
        exec("runas","regedit","",SW_SHOWNORMAL);
    }

    /// 打开 git 配置文件
    pub fn git(&self) {
        let git_path = env::var("USERPROFILE").unwrap() + "\\.gitconfig";
        if !Path::new(&git_path).exists() {
            println!("{} not exists",git_path)
        }else {
            // 打开文件
            exec("runas","C:\\Windows\\Notepad.exe",git_path.as_str(),SW_SHOWNORMAL);
        }
    }

    /// 打开env管理器
    /// command:rundll32.exe sysdm.cpl,EditEnvironmentVariables
    pub fn env(&self) {
        // 不能操作管理员环境变量
        // Command::new("rundll32").args(&["sysdm.cpl","EditEnvironmentVariables"]).status().expect("open env error");
        exec("runas","rundll32","sysdm.cpl EditEnvironmentVariables",SW_SHOWNORMAL);
        // Command::new("rundll32").args(&["sysdm.cpl","EditEnvironmentVariables"]).status().expect("open env error");
    }

    /// 打开服务管理器
    pub fn service(&self) {
        exec("runas","services.msc","",SW_SHOWNORMAL);
    }




}